local dianyi = fk.CreateSkill{
  name = "xiaobai__dianyi",
}

Fk:loadTranslationTable{
  ["xiaobai__dianyi"] = "典仪",
  [":xiaobai__dianyi"] = "体力值最大的角色受到伤害后，若没有体力值唯一最大的角色，"..
    "你可以令至少两名体力值相同的角色调整手牌至体力值数。",
  ["#xiaobai__dianyi_active"] = "典仪：你可以令至少两名体力值相同的角色将手牌调整至体力值数。"
}

dianyi:addEffect(fk.DamageInflicted , {
  is_delay_effect = true,
  mute = true,
  can_refresh = function (self, event, target, player, data)
    if not player:hasSkill(dianyi.name) then
      return false
    end
    local room = player.room
    for _, p in ipairs(room.alive_players) do
      if p.hp > target.hp then
        return false
      end
    end
    return true
  end,
  on_refresh = function (self, event, target, player, data)
    data.extra_data = data.extra_data or {}
    data.extra_data.xiaobai__dianyi = true
  end
})

dianyi:addEffect(fk.Damage, {
  anim_type = "control",
  can_trigger = function (self, event, target, player, data)
    if not player:hasSkill(dianyi.name) then
      return false
    end
    local room = player.room
    local maxTargets = {}
    local max_hp = -1
    for _, p in ipairs(room.alive_players) do
      if p.hp > max_hp then
        max_hp = p.hp
        maxTargets = {p}
      elseif p.hp == max_hp then
        table.insert(maxTargets, p)
      end
    end
    if #maxTargets > 1 then
      return data.extra_data and data.extra_data.xiaobai__dianyi
    end
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local done, dat = room:askToUseActiveSkill(player, {
      skill_name = "xiaobai__dianyi_active",
      prompt = "#xiaobai__dianyi_active",
      extra_data = {
        skillName = dianyi.name  
      }
    })
    if done and dat then
      event:setCostData(self, {targets = dat.targets})
      return true
    end
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local targets = event:getCostData(self).targets ---@type ServerPlayer[]
    room:sortByAction(targets)
    for _, p in ipairs(targets) do
      local num = p:getHandcardNum() - p.hp
      if num > 0 then
        room:askToDiscard(p, {
          max_num = num,
          min_num = num,
          skill_name = dianyi.name,
          include_equip = false,
          cancelable = false
        })
      elseif num < 0 then
        num = -num
        room:drawCards(p, num, dianyi.name)
      end
    end
  end
})

local dianyi_active = fk.CreateSkill{
  name = "xiaobai__dianyi_active"
}

dianyi_active:addEffect("active", {
  target_filter = function (self, player, to_select, selected, selected_cards, card, extra_data)
    if #selected == 0 then
      return to_select:getHandcardNum() ~= to_select.hp
    end
    return to_select.hp == selected[1].hp and to_select:getHandcardNum() ~= to_select.hp
  end,
  card_filter = Util.FalseFunc,
  feasible = function (self, player, selected, selected_cards, card)
    return #selected >= 2
  end,
  target_tip = function (self, player, to_select, selected, selected_cards, card, selectable, extra_data)
    if to_select:getHandcardNum() ~= to_select.hp then
      return tostring(to_select.hp)
    end
  end
})

return {dianyi, dianyi_active}